from django.db import models


class User(models.Model):
    """用户"""
    userid = models.AutoField(primary_key=True)
    username = models.CharField(unique=True, max_length=20)
    password = models.CharField(max_length=32)
    realname = models.CharField(max_length=20)
    sex = models.BooleanField(default=True)
    tel = models.CharField(unique=True, max_length=20)
    email = models.CharField(unique=True, max_length=255, blank=True, null=True)
    regdate = models.DateTimeField(auto_now_add=True)
    point = models.IntegerField(default=0)
    lastvisit = models.DateTimeField(blank=True, null=True)
    roles = models.ManyToManyField(to='Role', through='UserRole')

    class Meta:
        managed = False
        db_table = 'tb_user'


class District(models.Model):
    """地区"""
    distid = models.IntegerField(primary_key=True)
    parent = models.ForeignKey(to='self', on_delete=models.PROTECT, db_column='pid', blank=True, null=True)
    name = models.CharField(max_length=255)
    ishot = models.BooleanField(default=False)
    intro = models.CharField(max_length=255, blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        managed = False
        db_table = 'tb_district'


class Agent(models.Model):
    """经理人"""
    agentid = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255)
    tel = models.CharField(max_length=20)
    servstar = models.IntegerField()
    realstar = models.IntegerField()
    profstar = models.IntegerField()
    certificated = models.BooleanField(default=False)
    estates = models.ManyToManyField(to='Estate', through='AgentEstate')

    def __str__(self):
        return self.name

    class Meta:
        managed = False
        db_table = 'tb_agent'


class Estate(models.Model):
    """楼盘"""
    estateid = models.AutoField(primary_key=True)
    district = models.ForeignKey(to=District, on_delete=models.PROTECT, db_column='distid')
    name = models.CharField(max_length=255)
    hot = models.IntegerField(blank=True, null=True)
    intro = models.CharField(max_length=511, blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        managed = False
        db_table = 'tb_estate'


class AgentEstate(models.Model):
    """经理人楼盘中间实体"""
    agent_estate_id = models.AutoField(primary_key=True)
    agent = models.ForeignKey(to=Agent, on_delete=models.PROTECT, db_column='agentid')
    estate = models.ForeignKey(to=Estate, on_delete=models.PROTECT, db_column='estateid')

    class Meta:
        managed = False
        db_table = 'tb_agent_estate'
        unique_together = (('agent', 'estate'), )


class HouseType(models.Model):
    """户型"""
    typeid = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name

    class Meta:
        managed = False
        db_table = 'tb_house_type'


class HouseInfo(models.Model):
    """房源信息"""
    houseid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=50)
    area = models.IntegerField()
    floor = models.IntegerField()
    totalfloor = models.IntegerField()
    direction = models.CharField(max_length=10)
    price = models.IntegerField()
    priceunit = models.CharField(max_length=10)
    detail = models.CharField(max_length=511, default='')
    mainphoto = models.CharField(max_length=255)
    pubdate = models.DateField(auto_now_add=True)
    street = models.CharField(max_length=255)
    hassubway = models.BooleanField(default=False)
    isshared = models.BooleanField(default=False)
    hasagentfees = models.BooleanField(default=False)
    type = models.ForeignKey(to=HouseType, on_delete=models.PROTECT, db_column='typeid')
    user = models.ForeignKey(to=User, on_delete=models.PROTECT, db_column='userid')
    district_level2 = models.ForeignKey(to=District, on_delete=models.PROTECT, related_name='+', db_column='distid2')
    district_level3 = models.ForeignKey(to=District, on_delete=models.PROTECT, related_name='+', db_column='distid3')
    estate = models.ForeignKey(to=Estate, on_delete=models.PROTECT, db_column='estateid', blank=True, null=True)
    agent = models.ForeignKey(to=Agent, on_delete=models.PROTECT, db_column='agentid', blank=True, null=True)
    tags = models.ManyToManyField(to='Tag', through='HouseTag')

    class Meta:
        managed = False
        db_table = 'tb_house_info'


class HousePhoto(models.Model):
    """房源的图片"""
    photoid = models.AutoField(primary_key=True)
    house = models.ForeignKey(to=HouseInfo, on_delete=models.PROTECT, db_column='houseid')
    path = models.CharField(max_length=255)

    class Meta:
        managed = False
        db_table = 'tb_house_photo'


class Tag(models.Model):
    """标签"""
    tagid = models.AutoField(primary_key=True)
    content = models.CharField(max_length=20)

    def __str__(self):
        return self.content

    class Meta:
        managed = False
        db_table = 'tb_tag'


class HouseTag(models.Model):
    """房源标签中间实体"""
    house_tag_id = models.AutoField(primary_key=True)
    house = models.ForeignKey(to=HouseInfo, on_delete=models.PROTECT, db_column='houseid')
    tag = models.ForeignKey(to=Tag, on_delete=models.PROTECT, db_column='tagid')

    class Meta:
        managed = False
        db_table = 'tb_house_tag'
        unique_together = (('house', 'tag'), )


class Record(models.Model):
    """浏览记录"""
    recordid = models.BigAutoField(primary_key=True)
    user = models.ForeignKey(to=User, on_delete=models.PROTECT, db_column='userid')
    house = models.ForeignKey(to=HouseInfo, on_delete=models.PROTECT, db_column='houseid')
    recorddate = models.DateTimeField(auto_now=True)

    class Meta:
        managed = False
        db_table = 'tb_record'
        unique_together = (('user', 'house'), )


class LoginLog(models.Model):
    """登录日志"""
    logid = models.BigAutoField(primary_key=True)
    user = models.ForeignKey(to=User, on_delete=models.PROTECT, db_column='userid')
    ipaddr = models.CharField(max_length=255)
    logdate = models.DateTimeField(auto_now_add=True)
    devcode = models.CharField(max_length=255, default='')

    class Meta:
        managed = False
        db_table = 'tb_login_log'


class Role(models.Model):
    """角色"""
    roleid = models.AutoField(primary_key=True)
    rolename = models.CharField(max_length=255)
    privs = models.ManyToManyField(to='Privilege', through='RolePrivilege')

    def __str__(self):
        return self.rolename

    class Meta:
        managed = False
        db_table = 'tb_role'


class Privilege(models.Model):
    """权限"""
    privid = models.AutoField(primary_key=True)
    method = models.CharField(max_length=15)
    url = models.CharField(max_length=1024)
    detail = models.CharField(max_length=255, default='')

    def __str__(self):
        return self.detail

    class Meta:
        managed = False
        db_table = 'tb_privilege'


class UserRole(models.Model):
    """用户角色中间实体"""
    urid = models.AutoField(primary_key=True)
    user = models.ForeignKey(to=User, on_delete=models.PROTECT, db_column='userid')
    role = models.ForeignKey(to=Role, on_delete=models.PROTECT, db_column='roleid')

    class Meta:
        managed = False
        db_table = 'tb_user_role'
        unique_together = (('user', 'role'), )


class RolePrivilege(models.Model):
    """角色权限中间实体"""
    rpid = models.AutoField(primary_key=True)
    role = models.ForeignKey(to=Role, on_delete=models.PROTECT, db_column='roleid')
    priv = models.ForeignKey(to=Privilege, on_delete=models.PROTECT, db_column='privid')

    class Meta:
        managed = False
        db_table = 'tb_role_privilege'
        unique_together = (('role', 'priv'), )


class UserToken(models.Model):
    """用户身份令牌"""
    tokenid = models.AutoField(primary_key=True)
    token = models.CharField(max_length=32)
    user = models.OneToOneField(to=User, on_delete=models.PROTECT, db_column='userid')

    class Meta:
        managed = False
        db_table = 'tb_user_token'
